(function($, doc) {
	var user = app.getState();
	var uid = user.user_id;
	var defCoin = ethscan.getDefCoin();
	var defAddr = defCoin['address'];
	var initObj = {
		el: "#app",
		data: {
			dataArr: [],
			isShow: false,
			searchString: "",
			cartUrl: '',
			postData: {
				nameStart: '',
				codeStart: '',
				nameEnd: '',
				codeEnd: '',
				selsctType: 1,
				train_date: ''
			},
			zwmap: {

			},
			fullbox: false,
			trainList: [],
			isbuybox: false,
			getbuydata: [],
			gatcPriceBl: '',
			iszuowei: {
				swz: false,
				tdz: false,
				wz: false,
				yw: false,
				yz: false,
				edz: false,
				ydz: false
			},
			postTrain: {
				uid: '',
				train_date: '',
				from_station_code: '',
				to_station_code: '',
				checi: '',
				passengers: {
					passengerid: 1,
					passengersename: '',
					piaotype: 1,
					piaotypename: '成人票',
					passporttypeseid: 1,
					passporttypeseidname: '二代身份证',
					passportseno: '',
					price: '',
					zwcode: '',
					zwname: ''
				},
				payHash: '',
				gatcPrice: '',
				payPassword: ''
			}

		},
		filters: {
			formatMoney: function(value) {
				let mtel = value.toFixed(4) + 'GATC';
				return mtel;
			}
		},
		computed: {

			filteredArticles: function() {
				var articles_array = this.dataArr,
					searchString = this.searchString;

				if(!searchString) {
					return articles_array;
				}

				searchString = searchString.trim().toLowerCase();

				articles_array = articles_array.filter(function(item) {
					if(item.name.toLowerCase().indexOf(searchString) !== -1) {
						return item;
					}
				})

				// Return an array with the filtered data.
				return articles_array;;
			}
		},
		mounted: function() {
			var _this = this;
			_this.$nextTick(function() {

				_this.viewCick();
			});

		},
		methods: {
			viewCick: function() {
				var _this = this;

				_this.$http.get(_this.cartUrl + 'BuyApi/cityCode.html', {
					"id": Math.random() * 1000
				}).then(function(res) {
					if(res.body.error === 1) {
						_this.dataArr = res.body.data;

						_this.gatcPriceBl = res.body.gatcPrice;

					} else {
						alert('获取站点信息失败');
					}
				});

			},

			getprice: function(zwcode, selectint, price, gatcprice) {
				var _this = this;
				_this.postTrain.gatcPrice = gatcprice.toFixed(4);
				_this.postTrain.passengers.price = price;
				for(var Key in _this.iszuowei) {
					_this.iszuowei[Key] = false;
				}
				_this.iszuowei[selectint] = true;
				_this.postTrain.passengers.zwcode = zwcode;

			},
			buyTrainl: function() {
				var _this = this;
				var vr = _this.verifpost();
				if(vr.errors) {
					var passengers = JSON.stringify(_this.postTrain.passengers);

					console.log(passengers);
					
					_this.$http.post(
						_this.cartUrl + 'BuyApi/buyTrainl.html', {
							uid: _this.postTrain.uid,
							train_date: _this.postTrain.train_date,
							from_station_code: _this.postTrain.from_station_code,
							to_station_code: _this.postTrain.to_station_code,
							checi: _this.postTrain.checi,
							passengers: passengers,
							payHash: _this.postTrain.payHash,
							gatcPrice: _this.postTrain.gatcPrice,
							paypassword:_this.postTrain.payPassword,
							code : _this.postTrain.code,
							smstype : 6

						}, {
							'headers': {
								'Content-Type': 'x-www-form-urlencoded'
							}
						}
					).then(function(res) {
						//_this.fullbox = false;
						console.log(JSON.stringify(res.body));
						mui.alert("已提交:" + res.body.msg);
						_this.isbuybox = false;
					}, function(res) {

					});

				} else {
					alert(vr.msg);
				}

			},

			selectBox: function(i) { // 选择城市
				var _this = this;
				_this.isShow = true;
				_this.postData.selsctType = i;

			},

			clickBuy: function(items, index) {

				var _this = this;
				_this.isbuybox = true;
				_this.getbuydata = items;
				_this.postTrain.train_date = _this.postData.train_date;

				_this.postTrain.from_station_code = items.from_station_code;
				_this.postTrain.to_station_code = items.to_station_code;
				_this.postTrain.checi = items.train_code;

			},
			closeBuy: function() {
				var _this = this;
				_this.isbuybox = false;
			},
			selectCity: function(items, index, selsctType) {

				var _this = this;
				if(selsctType == 1) {
					_this.postData.nameStart = items.name;
					_this.postData.codeStart = items.code;
				} else {
					_this.postData.nameEnd = items.name;
					_this.postData.codeEnd = items.code;
				}

				_this.isShow = false;
			},
			selectTrain: function() {
				var _this = this;
				var verif = _this.verification();
				if(verif.errors) {

					_this.fullbox = true;

					_this.$http.post(
						_this.cartUrl + 'BuyApi/selectTrain.html', {
							train_date: _this.postData.train_date,
							from_station: _this.postData.codeStart,
							to_station: _this.postData.codeEnd
						}, {
							'headers': {
								'Content-Type': 'x-www-form-urlencoded'
							}
						}
					).then(function(res) {
						_this.fullbox = false;
						if(res.body.error == 1) {
							_this.trainList = res.body.data.list
						} else {
							mui.alert(res.body.msg);
						}

					}, function(res) {

					});
				} else {
					mui.alert(verif.msg);
				}
			},
			verifpost: function() {
				var _this = this;
				var reg_data = /(^\d{15}$)|(^\d{17}(\d|X)$)/;

				if(!reg_data.test(_this.postTrain.passengers.passportseno)) {
					return {
						errors: false,
						msg: '日期格式不正确'
					};
				}
				if(_this.postTrain.passengers.passengersename == '') {
					return {
						errors: false,
						msg: '乘车人姓名不能为空'
					};
				}
				if(_this.postTrain.gatcPrice == '') {
					return {
						errors: false,
						msg: '请选择需购买的车票类型'
					};
				}
				if(_this.postTrain.payPassword == '') {
					return {
						errors: false,
						msg: '请输入交易密码'
					};
				}
				if(_this.postTrain.code == '') {
					return {
						errors: false,
						msg: '请输入短信验证码'
					};
				}
				return {
					errors: true,
					msg: ''
				};

			},
			// 火车票下单

			verification: function() {
				var _this = this;
				var reg_data = /^(?:19|20)[0-9][0-9]-(?:(?:0[1-9])|(?:1[0-2]))-(?:(?:[0-2][1-9])|(?:[1-3][0-1]))$/;
				if(!reg_data.test(_this.postData.train_date)) {
					return {
						errors: false,
						msg: '日期格式不正确'
					};
				}
				if(_this.postData.codeStart == '') {
					return {
						errors: false,
						msg: '请选择始发站'
					};
				}
				if(_this.postData.codeEnd == '') {
					return {
						errors: false,
						msg: '请选择终点站'
					};
				}
				return {
					errors: true,
					msg: ''
				};

			}

		}

	};

	var indexApp = new Vue(initObj);

	//Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('meta[name=__hash__]').getAttribute('content');
	Vue.http.options.emulateJSON = true;
	indexApp.cartUrl = appurl;
	indexApp.postTrain.uid = uid;

	document.getElementById("pickDateBtn").addEventListener('tap', function() {

		var year = new Date().getFullYear();

		var month = new Date().getMonth();

		var day = new Date().getDate();

		var dDate = new Date();
		//设置当前日期（不设置默认当前日期）
		dDate.setFullYear(year, month, day);
		var minDate = new Date();
		//最小时间
		minDate.setFullYear(year, month, day);
		var maxDate = new Date();
		//最大时间
		maxDate.setFullYear(2016, 11, 31);
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var gz_month = (d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1);
			var gz_getdate = d.getDate() < 10 ? '0' + d.getDate() : d.getDate();
			//mui.toast('您选择的日期是:' + d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate());
			indexApp.postData.train_date = d.getFullYear() + "-" + gz_month + "-" + gz_getdate;

		}, function(e) {
			mui.toast("您没有选择日期");
		}, {
			title: '请选择日期',
			date: dDate,
			minDate: minDate,
			maxDate: maxDate
		});
	});
	var getCodeBox = document.getElementById('getCode');
	var c1 = 60;
	getCodeBox && getCodeBox.addEventListener('tap', function() {
		//console.log(appurl + 'chatapi/getsms');
		mui.post(appurl + 'ChatApi/getsms', {
			u: app.getState().user_id,
			smstype : 6
		}, function(res) {
			console.log(JSON.stringify(res));
			if(res.status == 1) {
				mui.toast('发送短信成功');
			} else {
				mui.toast(res.msg);
				getCodeBox.innerHTML = '获取验证码';
				getCodeBox.removeAttribute('disabled');
				c1 = 60;
				return;
			}
			var code1 = setInterval(function() {
				getCodeBox.innerHTML = c1 + '秒后重新获取';
				c1 = c1 - 1;
				if(c1 >= 0) {
					getCodeBox.setAttribute("disabled", "disabled");
				} else {
					getCodeBox.innerHTML = '获取验证码';
					getCodeBox.removeAttribute('disabled');
					c1 = 60;
					clearInterval(code1);
				}
			}, 1000);
		});
	})

}(mui, document));